package com.sscl.baselibrary.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.content.*
import android.util.AttributeSet
import android.view.*
import android.view.View.OnClickListener
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.sscl.baselibrary.R
import com.sscl.baselibrary.utils.BaseManager
import com.sscl.baselibrary.utils.DebugUtil
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit

/**
 * 数字键盘
 *
 * @author jackie
 */
class NumberInputMethodView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 成员变量
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /**
     * 数字1，2，3，4，5，6，7，8，9，0
     */
    private lateinit var numberTv1: TextView
    private lateinit var numberTv2: TextView
    private lateinit var numberTv3: TextView
    private lateinit var numberTv4: TextView
    private lateinit var numberTv5: TextView
    private lateinit var numberTv6: TextView
    private lateinit var numberTv7: TextView
    private lateinit var numberTv8: TextView
    private lateinit var numberTv9: TextView
    private lateinit var numberTv0: TextView

    /**
     * 清空
     */
    private lateinit var numberTvClear: TextView

    /**
     * 删除
     */
    private lateinit var numberTvDelete: RelativeLayout

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 私有成员常量
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    private val onClickListener: OnClickListener = OnClickListener { v ->
        when (v) {
            numberTv1 -> {
                pressKey(KeyEvent.KEYCODE_1)
            }

            numberTv2 -> {
                pressKey(KeyEvent.KEYCODE_2)
            }

            numberTv3 -> {
                pressKey(KeyEvent.KEYCODE_3)
            }

            numberTv4 -> {
                pressKey(KeyEvent.KEYCODE_4)
            }

            numberTv5 -> {
                pressKey(KeyEvent.KEYCODE_5)
            }

            numberTv6 -> {
                pressKey(KeyEvent.KEYCODE_6)
            }

            numberTv7 -> {
                pressKey(KeyEvent.KEYCODE_7)
            }

            numberTv8 -> {
                pressKey(KeyEvent.KEYCODE_8)
            }

            numberTv9 -> {
                pressKey(KeyEvent.KEYCODE_9)
            }

            numberTv0 -> {
                pressKey(KeyEvent.KEYCODE_0)
            }

            numberTvDelete -> {
                pressKey(KeyEvent.KEYCODE_DEL)
            }

            numberTvClear -> {
                if (context is Activity) {
                    val view = context.window.decorView.findFocus()
                    if (view is EditText) {
                        view.setText("")
                    }
                }
            }
        }
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 构造方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    private var scheduledExecutorService: ScheduledExecutorService? = null

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 私有方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /**
     * 初始化控件
     */
    private fun initViews() {
        numberTv1 = findViewById(R.id.number_tv_1)
        numberTv2 = findViewById(R.id.number_tv_2)
        numberTv3 = findViewById(R.id.number_tv_3)
        numberTv4 = findViewById(R.id.number_tv_4)
        numberTv5 = findViewById(R.id.number_tv_5)
        numberTv6 = findViewById(R.id.number_tv_6)
        numberTv7 = findViewById(R.id.number_tv_7)
        numberTv8 = findViewById(R.id.number_tv_8)
        numberTv9 = findViewById(R.id.number_tv_9)
        numberTv0 = findViewById(R.id.number_tv_0)
        numberTvClear = findViewById(R.id.number_tv_clear)
        numberTvDelete = findViewById(R.id.number_tv_delete)
    }

    /**
     * 初始化事件
     */
    @SuppressLint("ClickableViewAccessibility")
    private fun initEvents() {
        numberTv1.setOnClickListener(onClickListener)
        numberTv2.setOnClickListener(onClickListener)
        numberTv3.setOnClickListener(onClickListener)
        numberTv4.setOnClickListener(onClickListener)
        numberTv5.setOnClickListener(onClickListener)
        numberTv6.setOnClickListener(onClickListener)
        numberTv7.setOnClickListener(onClickListener)
        numberTv8.setOnClickListener(onClickListener)
        numberTv9.setOnClickListener(onClickListener)
        numberTv0.setOnClickListener(onClickListener)
        numberTvClear.setOnClickListener(onClickListener)
        numberTvDelete.setOnClickListener(onClickListener)
        numberTvDelete.setOnLongClickListener {
            DebugUtil.warnOut(TAG, "deleteTv onLongClick")
            startAutoDeleteTimer()
            true
        }
        numberTvDelete.setOnTouchListener { v, event ->
            if (event.action == MotionEvent.ACTION_UP) {
                stopAutoDeleteTimer()
            }
            false
        }
    }

    private fun stopAutoDeleteTimer() {
        scheduledExecutorService?.shutdownNow()
        scheduledExecutorService = null
    }

    private fun startAutoDeleteTimer() {
        stopAutoDeleteTimer()
        scheduledExecutorService = BaseManager.newScheduledExecutorService(1)
        scheduledExecutorService?.scheduleAtFixedRate({
            BaseManager.handler.post { pressKey(KeyEvent.KEYCODE_DEL) }
        }, 0, 100, TimeUnit.MILLISECONDS)
    }

    /**
     * 触发按键
     *
     * @param keycode 按键码
     */
    private fun pressKey(keycode: Int) {
        val activity = if (context is Activity) {
            context as Activity
        } else {
            return
        }
        activity.dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, keycode))
        activity.dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_UP, keycode))
    }

    companion object {
        private val TAG: String = NumberInputMethodView::class.java.simpleName
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 公开方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    init {
        inflate(context, R.layout.com_jackiepenghe_baselibrary_layout_number_input_method, this)
        initViews()
        initEvents()
    }
}